/**
 * @param {number[]} nums
 * @return {number}
 */
var longestConsecutive = function (nums) {
    let arr = nums.sort((a, b) => a - b)
    arr = [...new Set(arr)]
    let maxLength = 0
    let current = 0
    console.log(arr);
    for (let index = 0; index < arr.length; index++) {
        if ((arr[index] + 1) != arr[index + 1]) {
            maxLength = Math.max(index - current + 1, maxLength)
            current = index + 1
        }
    }
    return maxLength
};
console.log(longestConsecutive([1, 2, 0, 1]));
// 官网解答
; (function () {
    //return
    var longestConsecutive = function (nums) {
        let num_set = new Set();
        for (const num of nums) {
            num_set.add(num);
        }

        let longestStreak = 0;

        for (const num of num_set) {
            if (!num_set.has(num - 1)) {
                let currentNum = num;
                let currentStreak = 1;

                while (num_set.has(currentNum + 1)) {
                    currentNum += 1;
                    currentStreak += 1;
                }

                longestStreak = Math.max(longestStreak, currentStreak);
            }
        }

        return longestStreak;
    };


})();

